<%@ page import="java.util.regex.Pattern" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>身份证号提取器</title>
    <style type="text/css">
        #sfzh {
            width: 100%;
            color: blue;
            font-weight: bold;
            font-size: xx-large;
            font-family: "Courier New";
        }

        div {
            width: 260px;
            color: green;
        }
    </style>
</head>
<body>
<%
    String msg = "";
    String sfzh = request.getParameter("sfzh");
    String jg = "";
    String xb = "";
    String birth = "";
    String sx = "";
    String pattern = "[0-9]{17}[0-9Xx]";

    if (sfzh!=null&&Pattern.matches(pattern, sfzh)) {
        jg = sfzh.substring(0, 6);
        xb = sfzh.substring(16, 17);
        if (Integer.parseInt(xb) % 2 == 0) {
            xb = "女";
        } else {
            xb = "男";
        }
        birth = sfzh.substring(6, 14);
        String syear = birth.substring(0, 4);
        String month = birth.substring(4, 6);
        String day = birth.substring(6, 8);
        birth = syear + "年" + month + "月" + day + "日";
        int year = Integer.parseInt(syear);
        String ss = "猴、鸡、狗、猪、鼠、牛、虎、兔、龙、蛇、马、羊";
        String[] sxnum = ss.split("、");
        int mod = year % 12;
        sx = sxnum[mod];
    } else {
        msg = "请输入正确的身份证号";

    }

%>
<center>
    <H1>身份证信息提取器</H1>
    <h2><span style="color: red"><%=msg%></span></h2>
    <form action="idcard.jsp">
        <table border="1px" width="360px">
            <tr>
                <td colspan="2">
                    <input type="text" name="sfzh" id="sfzh" value="<%=sfzh==null?"":sfzh%>" placeholder="请输入身份证号">
                </td>
            </tr>
            <tr>
                <td>籍贯：</td>
                <td>
                    <div id="jg"><%=jg%>
                    </div>
                </td>
            </tr>
            <tr>
                <td>性别：</td>
                <td>
                    <div id="sex"><%=xb%>
                    </div>
                </td>
            </tr>
            <tr>
                <td>出生年月：</td>
                <td>
                    <div id="birthday"><%=birth%>
                    </div>
                </td>
            </tr>
            <tr>
                <td>属相：</td>
                <td>
                    <div id="sx"><%=sx%>
                    </div>
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <input type="submit" value="计算">
                </td>
            </tr>
        </table>
    </form>

</center>

</body>
</html>
